package rest

import (
	"github.com/ant0ine/go-json-rest/rest"
	"log"
	"net/http"
	"restmail/mail"
)

func CreatRest() *rest.Api{
	api := rest.NewApi()
	api.Use(rest.DefaultDevStack...)
	router, err := rest.MakeRouter(
		rest.Post("/sendmail", PostSendMail),
	)
	if err != nil {
		log.Fatal(err)
	}
	api.SetApp(router)
	return api
}

type Country struct {
	Code string
	Name string
}

func PostSendMail(w rest.ResponseWriter, r *rest.Request){
	param := new(mail.SendEmailParam)
	err := r.DecodeJsonPayload(param)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	log.Printf("%+v",param)
	mail.ChanMail <- param

	_ = w.WriteJson(map[string]string{"code": "200"})
}
